var httpSvc = require("./httpSvc");
var cf = require("../utils/config");
var q = require("querystring");
/*
* 请求规范
* @param d  json     请求数据
* @param h  json     请求头
* @param cb function callback函数
*
* */
//初始化数据
exports.data = function(d,h,cb){
    var path =cf.path_init+"/data?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("初始化数据：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("初始化数据：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//初始化电商数据
exports.shopData = function(d,h,cb){
    var path =cf.path_init+"/shopData?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("初始化电商数据：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("初始化电商数据：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//登录
exports.weblogin = function(d,h,cb){
    var path =cf.path_user+"/weblogin?mobile="+d.mobile+"&password="+d.password;
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("登录：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("登录：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//自媒体用户信息
exports.getUserInfo = function(d,h,cb){
    var path =cf.path_mediaAdv+"/getUserInfo?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("自媒体用户信息：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("自媒体用户信息：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//通过用户id 返回用户的自媒体列表
exports.getUserMediasList = function(d,h,cb){
    var path =cf.path_mediaAdv+"/getUserMediasList?uid="+d.uid+"&pageNumber="+d.pageNumber;
    if(d.sortId){
        path+="&sortId="+d.sortId;
    }
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("通过用户id 返回用户的自媒体列表：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("通过用户id 返回用户的自媒体列表：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//通过用户id 返回用户的商品列表
exports.getUserProductsList = function(d,h,cb){
    var path =cf.path_mediaAdv+"/getUserProductsList?"+q.stringify(d);
    if(d.sortId){
        path+="&sortId="+d.sortId;
    }
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("通过用户id 返回用户的商品列表：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("通过用户id 返回用户的商品列表：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//用户收货地址
exports.getUserDeliveryAddressList = function(d,h,cb){
    var path =cf.path_shop+"/getUserDeliveryAddressList?loginUid="+d.loginUid;
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("用户收货地址：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("用户收货地址：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//添加修改用户收货地址
exports.addUserDeliveryAddress = function(d,h,cb){
    var path =cf.path_shop+"/addUserDeliveryAddress?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("添加修改用户收货地址：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("添加修改用户收货地址：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//删除用户收货地址
exports.deleteUserDeliveryAddress = function(d,h,cb){
    var path =cf.path_shop+"/deleteUserDeliveryAddress?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("删除用户收货地址：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("删除用户收货地址：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//运费列表
exports.freightList = function(d,h,cb){
    var path =cf.path_mediaShop+"/freightList?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("运费列表：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("运费列表：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//保存运费(增加和修改运费)
exports.savefreight = function(d,h,cb){
    console.log("保存运费(增加和修改运费)22222");
    var paramStr="";
   /* console.log(paramStr+q.stringify(d));
    console.log(d);
    console.log(d.subpiece);*/
    var subpiece=d.subpiece.split(",");
    for(var i=0;i<subpiece.length;i++){
        paramStr+="subpiece="+subpiece[i]+"&"
    }
    var areafreightId=d.areafreightId.split(",");
    for(var i=0;i<areafreightId.length;i++){
        paramStr+="areafreightId="+areafreightId[i]+"&"
    }
    var subprovinceId=d.subprovinceId.split(",");
    for(var i=0;i<subprovinceId.length;i++){
        paramStr+="subprovinceId="+subprovinceId[i]+"&"
    }
    var submoney=d.submoney.split(",");
    for(var i=0;i<submoney.length;i++){
        paramStr+="submoney="+submoney[i]+"&"
    }
    var subaddPiece=d.subaddPiece.split(",");
    for(var i=0;i<subaddPiece.length;i++){
        paramStr+="subaddPiece="+subaddPiece[i]+"&"
    }
    var subaddMoney=d.subaddMoney.split(",");
    for(var i=0;i<subaddMoney.length;i++){
        paramStr+="subaddMoney="+subaddMoney[i]+"&"
    }
    // console.log(paramStr+"paramStr");
    // console.log("d  "+d);
    // d.remove("areafreightId");
    // d.remove("subprovinceId");
    // d.remove("submoney");
    // d.remove("subaddPiece");
    // d.remove("subaddMoney");
    // d.remove("subpiece");
    console.log("qq22  "+q.stringify(d));
    console.log(paramStr)
    var path =cf.path_mediaShop+"/savefreight?"+paramStr+"loginUid="+d.loginUid+"&name="+d.name+"&isExcept="+d.isExcept+"&provinceId="+d.provinceId+"&freightId="+d.freightId+"&piece="+d.piece+"&money="+d.money+"&addPiece="+d.addPiece+"&addMoney="+d.addMoney;
    console.log(path)
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("保存运费(增加和修改运费)：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("保存运费(增加和修改运费)：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//删除运费
exports.deletefreight = function(d,h,cb){
    var path =cf.path_mediaShop+"/deletefreight?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("删除运费：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("删除运费：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//删除子运费
exports.deleteareafreight = function(d,h,cb){
    var path =cf.path_mediaShop+"/deleteareafreight?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("删除子运费：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("删除子运费：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//获取个人商品详情
exports.getMediaProductById = function(d,h,cb){
    var path =cf.path_shop+"/getMediaProductById?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("获取个人商品详情：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("获取个人商品详情：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//预发短信
exports.presendMessage = function(d,h,cb){
    var path =cf.path_user+"/presendMessage?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("预发短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("预发短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//注册短信
exports.sendregistermessage = function(d,h,cb){
    var path =cf.path_user+"/sendregistermessage?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("注册短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("注册短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//注册
exports.register = function(d,h,cb){
    var path =cf.path_user+"/register?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("注册：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("注册：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//登录短信
exports.sendloginmessage = function(d,h,cb){
    var path =cf.path_user+"/sendloginmessage?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("登录短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("登录短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//忘记密码短信
exports.sendforgetmessage = function(d,h,cb){
    var path =cf.path_user+"/sendforgetmessage?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("忘记密码短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("忘记密码短信：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//设置忘记密码
exports.setForgetCode = function(d,h,cb){
    var path =cf.path_user+"/setForgetCode?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("设置忘记密码：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("设置忘记密码：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//验证码登录
exports.webloginByCode = function(d,h,cb){
    var path =cf.path_user+"/webloginByCode?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("验证码登录：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("验证码登录：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//完善个人信息-第一步
exports.setFirstInfo = function(d,h,cb){
    var path =cf.path_user+"/setFirstInfo?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("完善个人信息-第一步：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("完善个人信息-第一步：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//获取个人标签
exports.getUserLabel = function(d,h,cb){
    var path =cf.path_user+"/getUserLabel?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("获取个人标签：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("获取个人标签：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//保存个人标签
exports.saveUserLabel = function(d,h,cb){
    var path =cf.path_user+"/saveUserLabel?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("保存个人标签：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body ));
        } else {
            global.loginfo("保存个人标签：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//推荐用户
exports.recommendFans = function(d,h,cb){
    var path =cf.path_user+"/recommendFans?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("推荐用户：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body));
        } else {
            global.loginfo("推荐用户：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//获取用户乐币
exports.getUserIntegral = function(d,h,cb){
    var path =cf.path_user+"/getUserIntegral?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("获取用户乐币：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body));
        } else {
            global.loginfo("获取用户乐币：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};
//获取配置文件
exports.releaseConfig = function(d,h,cb){
    var path ="/v1/release/releaseConfig?"+q.stringify(d);
    var opt = httpSvc.makeOpt(cf.server_host,cf.server_port, path, "post");
    httpSvc.httpReq(opt, function (b) {
        if (b.json.code !=0) {
            global.logerror("获取配置文件：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + (b.body));
        } else {
            global.loginfo("获取配置文件：" + JSON.stringify(opt) + " .posts. " + JSON.stringify(d) + " .back. " + b.body);
        }
        cb(b)
    }, d);
};